package cn.fzm.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Dremeue
 * @create 2019-10-07 20:06
 **/
public class SysExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {
        ex.printStackTrace();

        SysException e = null;
        // 获取到异常类型
        if (ex instanceof SysException) {
            e = (SysException) ex;
        } else {
            e = new SysException("请联系管理员");
        }

        ModelAndView mv = new ModelAndView();

        // 存入错误信息
        mv.addObject("message", e.getMessage());
        mv.setViewName("error");

        return mv;
    }

}
